package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/03 11:37
 **/
public class Day19_3_Num13_RomanToInt {
    class Solution {
        public int romanToInt(String s) {
            int target = 0;
            char[] roman = s.toCharArray();
            for(int i = 0;i < roman.length;i++){
                if(roman[i] == 'M') target += 1000;
                if(roman[i] == 'D') target += 500;
                if(roman[i] == 'L') target += 50;
                if(roman[i] == 'V') target += 5;

                if(roman[i] == 'C') {
                    if((i+1) < roman.length && roman[i+1] == 'M'){
                        target += 900;i++;
                    }else if((i+1) < roman.length && roman[i+1] == 'D'){
                        target += 400;i++;
                    }else target += 100;
                }
                if(roman[i] == 'X') {
                    if((i+1) < roman.length && roman[i+1] == 'C'){
                        target += 90;i++;
                    }else if((i+1) < roman.length && roman[i+1] == 'L'){
                        target += 40;i++;
                    }else target += 10;
                }
                if(roman[i] == 'I') {
                    if((i+1) < roman.length && roman[i+1] == 'X'){
                        target += 9;i++;
                    }else if((i+1) < roman.length && roman[i+1] == 'V'){
                        target += 4;i++;
                    }else target += 1;
                }
            }
            return target;
        }
    }
}